package cc.mrbird.febs.buss.controller;

import cc.mrbird.febs.buss.entity.ConnPolicyFile;
import cc.mrbird.febs.buss.entity.HallInfo;
import cc.mrbird.febs.buss.entity.TFile;
import cc.mrbird.febs.buss.service.IConnPolicyFileService;
import cc.mrbird.febs.buss.service.IHallInfoService;
import cc.mrbird.febs.buss.service.ITFileService;
import cc.mrbird.febs.buss.util.StringUtil;
import cc.mrbird.febs.common.annotation.ControllerEndpoint;
import cc.mrbird.febs.common.utils.FebsUtil;
import cc.mrbird.febs.common.entity.FebsConstant;
import cc.mrbird.febs.common.controller.BaseController;
import cc.mrbird.febs.common.entity.FebsResponse;
import cc.mrbird.febs.common.entity.QueryRequest;
import cc.mrbird.febs.buss.entity.Policy;
import cc.mrbird.febs.buss.service.IPolicyService;
import cc.mrbird.febs.system.entity.Dept;
import cc.mrbird.febs.system.service.IDeptService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.wuwenze.poi.ExcelKit;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.jws.WebParam;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.util.*;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;

/**
 *  Controller
 *
 * @author Lujingyuan
 * @date 2020-11-26 10:26:58
 */
@Slf4j
@Validated
@Controller
public class PolicyController extends BaseController {

    @Autowired
    private IPolicyService policyService;
    @Autowired
    private IHallInfoService hallInfoService;
    @Autowired
    private IDeptService deptService;
    @Autowired
    private ITFileService fileService;
    @Autowired
    private IConnPolicyFileService connPolicyFileService;

    /**
     * 政策法规页面
     * @return
     */
    @GetMapping("policy/home")
    public String home(String hallCode){
        if(StringUtil.isNotNull(hallCode)){
            return FebsUtil.view("policy/"+hallCode+"_home");
        }
        return FebsUtil.view("policy/home");
    }

    /**
     * 部门列表
     * @param model
     * @return
     */
    @GetMapping("policy/deptList")
    public String deptList(Model model,String hallCode){
        List<Dept> deptList = null;
        //查询能显示的部门
        QueryWrapper<Dept> queryWrapper = new QueryWrapper<>();
        QueryWrapper<HallInfo> hallInfoQueryWrapper = new QueryWrapper<>();
        if(StringUtil.isNotNull(hallCode)){
            hallInfoQueryWrapper.eq("hall_code",hallCode);
            HallInfo hallInfo = hallInfoService.getOne(hallInfoQueryWrapper);
            queryWrapper.eq("HALL_ID",hallInfo.getId());
        }else{
            hallCode = "zz_zdxq";
            hallInfoQueryWrapper.eq("hall_code",hallCode);
            HallInfo hallInfo = hallInfoService.getOne(hallInfoQueryWrapper);
            queryWrapper.eq("HALL_ID",hallInfo.getId());
        }

        queryWrapper.eq("is_show",1);
        deptList = deptService.list(queryWrapper);

        model.addAttribute("deptList",deptList);
        return FebsUtil.view("policy/deptList");
    }
    /**
     * 点击部门对应的页面跳转
     * @param deptId
     * @param model
     * @return
     */
    @GetMapping("policy/policyList/{deptId}")
    public String toResult(@PathVariable("deptId") String deptId,Model model){
        Dept dept = deptService.getById(deptId);
        List<Policy> list = policyService.getListByDeptId(deptId);
        model.addAttribute("dept",dept);
        model.addAttribute("policyList",list);
        return FebsUtil.view("policy/policyList");
    }


    /**
     * 前端页面路由地址
     * @param model
     * @return
     */
    @GetMapping("policy/index")
    public String index(Model model){
        List<Dept> deptList = deptService.list();
        model.addAttribute("deptList",deptList);
        return FebsUtil.view("policy/index");
    }



    @GetMapping(FebsConstant.VIEW_PREFIX + "policy")
    public String policyIndex(){
        return FebsUtil.view("policy/policy");
    }

    @GetMapping("policy")
    @ResponseBody
    @RequiresPermissions("policy:list")
    public FebsResponse getAllPolicys(Policy policy) {
        return new FebsResponse().success().data(policyService.findPolicys(policy));
    }

    @GetMapping("policy/list")
    @ResponseBody
    public FebsResponse policyList(QueryRequest request, HttpServletRequest req, Policy policy) {
        Enumeration enu=req.getParameterNames();
        HashMap<String,String> map=new HashMap<String,String>();
        while(enu.hasMoreElements()){
            String paraName=(String)enu.nextElement();
            map.put(paraName,req.getParameter(paraName));
        }
        Map<String, Object> dataTable = getDataTable(this.policyService.findPolicyDetail(request, map));
        return new FebsResponse().success().data(dataTable);
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "policy/add")
    public String goPolicyAdd(Model model) {
        List<Dept> deptList = deptService.list();
        model.addAttribute("deptList",deptList);
        return FebsUtil.view("policy/policyAdd");
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "policy/update/{id}")
    public String goPolicyUpdate(@PathVariable String id, Model model) {
        Policy obj= policyService.getById(id);
        ArrayList<TFile> files = new ArrayList<>();
        QueryWrapper<ConnPolicyFile> wrapper = new QueryWrapper<>();
        wrapper.eq("policy_id",id);
        List<ConnPolicyFile> connPolicyFiles = connPolicyFileService.list(wrapper);
        for (ConnPolicyFile connPolicyFile : connPolicyFiles) {
            TFile file = fileService.getById(connPolicyFile.getFileId());
            files.add(file);
        }
        model.addAttribute("obj",obj);
        model.addAttribute("files",files);
        return FebsUtil.view("policy/policyUpdate");
    }

    @ControllerEndpoint(operation = "新增Policy", exceptionMessage = "新增Policy失败")
    @PostMapping("policy")
    @ResponseBody
    public FebsResponse addPolicy(@Valid Policy policy,String fileId) {
        this.policyService.createPolicy(policy);
        ConnPolicyFile connPolicyFile = new ConnPolicyFile();
        connPolicyFile.setPolicyId(policy.getId());
        connPolicyFile.setFileId(Long.parseLong(fileId));
        connPolicyFileService.createConnPolicyFile(connPolicyFile);
        return new FebsResponse().success();
    }

    @GetMapping("policy/delete/{objIds}")
    @ControllerEndpoint(operation = "删除", exceptionMessage = "删除失败")
    @ResponseBody
    public FebsResponse deletePolicy( @PathVariable String objIds) {
        String[] ids = objIds.split(StringPool.COMMA);
        this.policyService.deletePolicy(ids);
        return new FebsResponse().success();
    }

    @ControllerEndpoint(operation = "修改Policy", exceptionMessage = "修改Policy失败")
    @PostMapping("policy/update")
    @ResponseBody
    public FebsResponse updatePolicy(Policy policy,String fileId) {
        this.policyService.updatePolicy(policy);
        if(StringUtil.isNotNull(fileId)){
            ConnPolicyFile connPolicyFile = new ConnPolicyFile();
            connPolicyFile.setPolicyId(policy.getId());
            connPolicyFile.setFileId(Long.parseLong(fileId));
            connPolicyFileService.createConnPolicyFile(connPolicyFile);
        }

        return new FebsResponse().success();
    }
}
